package cn.dqy.gotanserver.support.msghandle.impl;

import cn.dqy.gotanserver.config.ConfigClass;
import cn.dqy.gotanserver.dto.ChannelInfo;
import cn.dqy.gotanserver.dto.GoTanInet;
import cn.dqy.gotanserver.dto.GoTanObject;
import cn.dqy.gotanserver.service.CoreServer;
import cn.dqy.gotanserver.support.SupportHelper;
import cn.dqy.gotanserver.support.msghandle.MsgProcessInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Queue;

//处理视频流消息
@Component
public class VideoStreamMsgProcess implements MsgProcessInterface {
    static Logger logger = LoggerFactory.getLogger(VideoStreamMsgProcess.class);

    @Autowired
    CoreServer coreServer;
    @Autowired
    ConfigClass configClass;
    @Override
    public void process(GoTanInet inetPack, GoTanObject headPack, String ipAndPortKey) {
        logger.info("视频推流消息处理");
        String remark = headPack.getRemark();
        String psId = remark.split("::")[1];
        int streamId = Integer.parseInt(psId);
        logger.info("处理流文件,streamId={}",streamId);

        ChannelInfo channelInfo = coreServer.getChannelMap().get(ipAndPortKey);
        if(channelInfo!=null){
            pullStreamHandle(inetPack,streamId,channelInfo);
        }
        return;
    }

    private void pullStreamHandle(GoTanInet inet,int streamId,ChannelInfo channelInfo){
        String saveFile=channelInfo.getPullStreamPath()+streamId+configClass.getPicType();
        Queue<String> picQueue = channelInfo.getPicQueue();
        saveFile(saveFile,inet.getPic_data());
        picQueue.add(saveFile);
    }
}
